// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var userLoginData = new Object();//require("zqddn_zhb_Common"); //全局变量

cc.Class({
    extends: cc.Component,

    properties: {
        posNode: cc.Node, //目标位置节点
        urlIndex: 1, //按钮图片链接索引
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    // update (dt) {},
    
    
    onEnable: function(){
        cc.log("userInfo onEnable-1");
        var self = this;
        //按钮坐标
        //self.left = width/2 - btWidth/2;
        //self.top = height/2 - btHeight/2;
        //按钮图片
        if(this.urlIndex == 1){
            self.url = "http://115.182.62.13:82/test/timg.jpg";
        }else if(this.urlIndex == 2){
            self.url = "http://115.182.62.13:82/test/timg.jpg";
        }else if(this.urlIndex == 3){
            self.url = "http://115.182.62.13:82/test/timg.jpg";
        }
        if(self.button){
            self.button.show();
        }else{
            if(!(cc.sys.platform === cc.sys.WECHAT_GAME)) return;
            wx.getSystemInfo({
                success(res){
                    var width = res.screenWidth;
                    var height = res.screenHeight;
                    var btWidth = 424/2.2; //这里只是简单的认为Dpr 2.2
                    var btHeight = 148/2.2;
                    self.button = wx.createUserInfoButton({
                        type: "image",
                        image: self.url,
                        style: {
                            left: width/2 - btWidth/2,
                            top: height/2 - btHeight/2 + 200,
                            width: btWidth,
                            height: btHeight,
                        }
                    })
                    self.button.onTap((res1) => {
                        wx.getSetting({
                            success(auths){
                                if(auths.authSetting["scope.userInfo"]){
                                    console.log("==已经授权===")
                                    wx.login({
                                        success(res2){ 
                                            //获得个人信息
                                            wx.getUserInfo({
                                                withCredentials: true,
                                                lang: 'zh_CN',
                                                success(res3){
                                                    userLoginData.avatarUrl = res3.userInfo.avatarUrl;
                                                    userLoginData.nickName = res3.userInfo.nickName;
                                                    userLoginData.iv = res3.userInfo.iv;
                                                    userLoginData.encryptedData = res3.userInfo.encryptedData;
                                                    cc.sys.localStorage.setItem("zqddn_zhb_userLoginData", JSON.stringify(userLoginData));              
                                                },
                                                fail(){
                                                    console.log("login:获取自己的信息失败");
                                                }
                                            })
                                            self.node.active = false; 
                                        }
                                    })
                                }else{
                                    console.log("==拒绝授权===")
                                    self.node.active = false; 
                                }
                            }
                        })
                    })
                    self.button.show()
                }
            })
        }
    },
    
    onDisable: function(){
        if(this.button){
            this.button.hide();
        }
    }
});
